import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

let routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    // alias: '/', // 别名
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'), // 页面引入方法二
  },
];

// 批量引入模块页面路由
const moduleFiles = require.context('./modules', true, /\.js$/);
moduleFiles.keys().forEach((modulePath) => {
  routes.push(...moduleFiles(modulePath).default);
});

export default new Router({
  mode: 'history',
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      if (from.meta.keepAlive) {
        const selector = document.getElementById('scrollbar') || {};
        from.meta.savedPosition = selector.scrollTop || 0;
      }
      return { x: 0, y: to.meta.savedPosition || 0 };
    }
  },
});
